<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="orderList">
      <el-table-column label="等级ID" align="center" prop="levelId" />
      <el-table-column label="等级名称" align="center" prop="levelName" />
      <el-table-column label="任意消费金额" align="center" prop="consumptionAmount" />
      <el-table-column label="仅注册" align="center" prop="registerOnly">
        <template slot-scope="scope">
          <span>{{ scope.row.registerOnly?'是':'否' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购买指定产品(ID)" align="center" prop="buyProduct" />
      <el-table-column label="推荐指定身份规则" align="center" prop="referrerLevel">
        <template slot-scope="scope">
          <div v-if="scope.row.referrerLevel">
            <div>推荐等级：{{ scope.row.referrerLevel.levelId}}</div>
            <div>推荐人数：{{ scope.row.referrerLevel.count}}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="消费/佣金折扣(%)" align="center" prop="discount">
        <template slot-scope="scope">
          <span>{{ Number(scope.row.discount)*100 }}%</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="等级名称" prop="levelName">
          <el-input v-model="form.levelName" placeholder="请输入等级名称" />
        </el-form-item>
        <el-form-item label="任意消费金额" prop="consumptionAmount">
          <el-input v-model="form.consumptionAmount" type="number" placeholder="请输入任意消费金额" />
        </el-form-item>
        <el-form-item label="仅注册" prop="registerOnly">
          <el-radio-group v-model="form.registerOnly">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="购买指定产品(ID)" prop="buyProduct">
          <el-input v-model="form.buyProduct" type="number" placeholder="请输入购买指定产品(ID)" />
        </el-form-item>
        <el-form-item label="推荐指定身份规则">
          <template v-if="form.referrerLevel">
            <el-input v-model="form.referrerLevel.levelId" placeholder="请输入推荐等级" />
            <el-input style="margin-top: 5px;" v-model="form.referrerLevel.count" placeholder="请输入推荐人数" />
          </template>
        </el-form-item>
        <el-form-item label="消费/佣金折扣(%)" prop="discount">
          <el-input v-model="form.discount" type="number" :min="0" :max="100" placeholder="请输入消费/佣金折扣(%)" @blur="changeNum" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import {
  productCateList,
  insertCate,
  detailCate,
  modifyCate,
  delCate,
} from "@/api/product/productCate";
import { userLevelList,changeUserLevelInfo } from '@/api/customer/customer'
import { getSassToken } from "@/utils/utils";
import { Debounce } from '@/utils/tools.js';
import imageDrapUpload from '@/components/Upload';
export default {
  name: "productCate",
  components:{
    imageDrapUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      typeList: [],
      // 弹出层标题
      title: "订单发货",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        cateName: "",
      },
      // 表单参数
      form: {
        levelName: null,
        consumptionAmount: null,
        registerOnly: null,
        buyProduct: null,
        discount: null,
        referrerLevel:{}
      },
      // 表单校验
      rules: {
        levelName: [
          { required: true, message: "请输入等级名称", trigger: "blur" }
        ],
        consumptionAmount: [
          { required: true, message: "请输入任意消费金额", trigger: "blur" }
        ],
        registerOnly: [
          { required: true, message: "请选择是否仅注册", trigger: "change" }
        ],
        discount: [
          { required: true, message: "请输入消费/佣金折扣(1-100)", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeNum(){
      if(this.form.discount > 100){
        this.form.discount = 100
      }
      if(this.form.discount < 0){
        this.form.discount = 0
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      userLevelList(this.queryParams).then((response) => {
        response.data.records.forEach(item => {
          if(item.referrerLevel && item.referrerLevel.length > 0){
              item.referrerLevel = JSON.parse(item.referrerLevel)
          }
        })
        this.orderList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete:Debounce(function (row) {
      this.$modal.confirm('是否确认删除名称为"' + row.cateName + '"的数据项？').then(function() {
        return delCate(row.cateId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },500),
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate:Debounce(function (row) {
      this.reset();
      let data = JSON.parse(JSON.stringify(row))
      data.discount = Number(data.discount)*100;
      if(!data.referrerLevel){
        data.referrerLevel= {
          levelId: null,
          count: null
        }
      }
      console.log(data,'==================')
      this.form = data;
      this.open = true;
      this.title = "修改等级信息";
    },500),
    /** 提交按钮 */
    submitForm:Debounce(function (row) {
      let that = this
      this.$modal.confirm('是否确认修改数据？').then(function() {
        that.$refs["form"].validate((valid) => {
          if (valid) {
            let num = 0;
            if(that.form.discount > 0){
              num = Number(that.form.discount) / 100
            }
            let data = {
              levelName: that.form.levelName,
              consumptionAmount:  that.form.consumptionAmount,
              registerOnly: that.form.registerOnly,
              buyProduct:  that.form.buyProduct,
              referrerLevel:  JSON.stringify(that.form.referrerLevel),
              discount:  num,
              levelId:  that.form.levelId
            };
            changeUserLevelInfo(data).then((response) => {
              that.$modal.msgSuccess("修改成功");
              that.open = false;
              that.getList();
            });
            
          }
        });
      }).catch(() => {});

    },500),
  },
};
</script>
<style lang="scss" scoped>
</style>